package com.unisv.dj;

import java.util.Timer;
import java.util.TimerTask;

import com.unisv.dj.R.id;
import com.unisv.dj.util.Command;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * <p>
 * init activity
 * </p>
 * 
 * @author fanzhen
 * @time 8:04:13 PM Sep 23, 2011
 * @email fanzhen.meng@gmail.com
 */
public class ManualActivity extends Activity {

	// private static final String TAG = "AutoActivity";
	BroadcastReceiver commandReceiver;
	IntentFilter movementFilter;
	boolean timerFlag = false;
	int currentSpeed;
	int counter = 1;
	private CountDownTimer countDownTimer;
	private Timer autoPlusShort;
	private ImageView imgPlus;
	private ImageView imgStart;
	private ImageView imgStop;
	private ImageView imgShort;
	private ImageView imgMusic;
	private ImageView imgVideo;
	private TextView tvcountdown;
	private TextView tvspeed;

	TimerTask task;
	Handler eventHandler;
	int ChangeFlag;

	void initStart() {
		timerFlag = false;
		countDownTimer.cancel();
		currentSpeed = 1;
		tvspeed.setText("Speed:" + String.valueOf(currentSpeed));
		tvcountdown.setText("Count:10/0");
	}

	void changeSpeed(int viewId) {
		final int tempSpeed = currentSpeed;
		if (viewId == R.id.imgPlus) {
			if (currentSpeed + counter <= 50) {
				currentSpeed = currentSpeed + counter;
				tvspeed.setText("Speed:" + String.valueOf(currentSpeed));
			}
		} else if (viewId == R.id.imgShort) {
			if (currentSpeed - counter >= 1) {
				currentSpeed = currentSpeed - counter;
				tvspeed.setText("Speed:" + String.valueOf(currentSpeed));
			}
		}
		if (timerFlag && tempSpeed != currentSpeed)
			Command.changeSpeed(currentSpeed, currentSpeed);
	}
	
	void finishPlusOrShort()
	{
		if (task != null)
			task.cancel();
		if (autoPlusShort != null)
			autoPlusShort.purge();
		ChangeFlag = 0;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.manual);
		imgPlus = (ImageView) this.findViewById(R.id.imgPlus);
		imgStart = (ImageView) this.findViewById(R.id.imgStart);
		imgStop = (ImageView) this.findViewById(R.id.imgStop);
		imgShort = (ImageView) this.findViewById(R.id.imgShort);
		imgMusic = (ImageView) this.findViewById(R.id.imgMusic);
		imgVideo = (ImageView) this.findViewById(R.id.imgVideo);
		tvcountdown = (TextView) this.findViewById(R.id.tvcountdownid);
		tvspeed = (TextView) this.findViewById(id.tvspeedid);

		autoPlusShort = new Timer("autoPlusShort");

		eventHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1:
					changeSpeed(ChangeFlag);
					break;
				default:
					break;
				}
				super.handleMessage(msg);
			}
		};

		class CountTask extends TimerTask {
			@Override
			public void run() {
				Message msg = new Message();
				msg.what = 1;
				msg.arg1 = (int) (Math.random() * 10d) + 90;
				eventHandler.sendMessage(msg);
			}
		}

		imgPlus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Plus
				changeSpeed(arg0.getId());
			}
		});

		imgPlus.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View arg0) {
				ChangeFlag = R.id.imgPlus;
				task = new CountTask();
				autoPlusShort.scheduleAtFixedRate(task, 10, 100);
				return false;
			}
		});

		imgPlus.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (arg1.getAction() == MotionEvent.ACTION_UP && task != null) {
					finishPlusOrShort();
				}
				return false;
			}
		});

		imgStart.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Start
				if (!timerFlag) {
					timerFlag = true;
					countDownTimer.start();
					// Command.start();
					Command.changeSpeed(currentSpeed, currentSpeed);
				}
			}
		});

		imgStop.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Stop
				initStart();
				timerFlag = false;
				// Command.stopShort();
				Command.changeSpeed(0, 0);
			}
		});

		imgStop.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View arg0) {
				// TODO Auto-generated method stub
				initStart();
				timerFlag = false;
				// Command.stopLong();
				Command.changeSpeed(0, 0);
				finish();
				return false;
			}
		});

		imgShort.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Short
				changeSpeed(arg0.getId());
			}
		});

		imgShort.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View arg0) {
				ChangeFlag = R.id.imgShort;
				task = new CountTask();
				autoPlusShort.scheduleAtFixedRate(task, 10, 100);
				return false;
			}
		});

		imgShort.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (arg1.getAction() == MotionEvent.ACTION_UP && task != null) {
					finishPlusOrShort();
				}
				return false;
			}
		});

		imgMusic.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Music
				Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
				startActivity(intent);
			}
		});

		imgVideo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Vodeo
				Intent intent = new Intent();  
				intent.setAction(android.content.Intent.ACTION_VIEW);
				intent.setDataAndType(Uri.EMPTY, "video/*");
				startActivity(intent);
			}
		});

		countDownTimer = new CountDownTimer(10 * 60 * 1000L, 1000L) {
			@Override
			public void onTick(long millisUntilFinished) {
				int currentMinute = (int) millisUntilFinished / 1000 / 60;
				tvcountdown.setText(String.format("Count:%1$d/%2$d", currentMinute, millisUntilFinished / 1000 % 60));
			}

			@Override
			public void onFinish() {
				tvcountdown.setText("DONE!");
				timerFlag = false;
				// Command.stopShort();
				Command.changeSpeed(0, 0);
			}
		};

		initStart();
	}

	@Override
	public void onResume() {
		if (movementFilter == null)
			movementFilter = new IntentFilter(ServerService.COMMAND_COMING);
		if (commandReceiver == null)
			commandReceiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {
					// Log.i(TAG, "msg from service.............." + command);
					byte[] comm = intent.getByteArrayExtra(ServerService.COMMAND_TAG);
					if (comm.length == 4 && comm[0] == 111) {
						switch (comm[3]) {
						case 1:
							imgStart.performClick();
							break;
						case 2:
							imgStop.performClick();
							break;
						case 3:
							imgStop.performLongClick();
							break;
						case 4:
							imgPlus.performClick();
							break;
						case 5:
							imgShort.performClick();
							break;
						case 6:
							imgPlus.performClick();
							break;
						case 7:
							imgShort.performClick();
							break;
						case 8:
							imgPlus.performLongClick();
							break;
						case 9:
							imgShort.performLongClick();
							break;
						case 10:
							imgPlus.performLongClick();
							break;
						case 11:
							imgShort.performLongClick();
							break;
						case 12:
							// 左松开
							finishPlusOrShort();
						case 13:
							// 右松开
							finishPlusOrShort();
						case 14:
							// 上松开
							finishPlusOrShort();
						case 15:
							// 下松开
							finishPlusOrShort();
							break;

						default:
							break;
						}
					}
				}
			};
		registerReceiver(commandReceiver, movementFilter);
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (commandReceiver != null) {
			unregisterReceiver(commandReceiver);
			commandReceiver = null;
		}
		if (movementFilter != null)
			movementFilter = null;
		super.onDestroy();
	}

}
